package main;


import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import autoTest.*;
import autoTest.Configuration;
import autoTest.ConfigurationReader;
import autoTest.Holder;
import autoTest.database.DataManager;
import autoTest.device.DeviceInfo;
import autoTest.request.EndPart;
import autoTest.request.GetTaskPart;
import autoTest.request.QuerryBlackListPart;
import autoTest.request.QuerryPart;
import autoTest.request.QuerryResultPart;
import autoTest.request.ReportPart;
import autoTest.request.ReportStatusPart;
import autoTest.request.SetBlackListPart;
import autoTest.request.StartPart;
import autoTest.request.StartRemotePart;

public class ServerMain {
	
	public static Map<String, Holder> allReponseMap = new ConcurrentHashMap<String, Holder>();	//对应的requestId和请求结果
	public static Map<String, String> allDeviceReqMap = new ConcurrentHashMap<String, String>();		//当前设备运行的 RequestId（只要请求返回立刻清空）
	public static Map<String, DeviceInfo> allDeviceInfo = new ConcurrentHashMap<String, DeviceInfo>();		//所有设备信息
	private static String casePath = new String("");
	private static ServletContextHandler context = null;
	private static int basicPathLength = -1;
	public static int getBasicPathLength() {
		return basicPathLength;
	}

	public static void main(String[] args) throws Exception {
		
		DataManager.createDatabase();

        Configuration conf = ConfigurationReader.readConfiguration();
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setMaxThreads(50);		//最大同时维护 50 个线程
        Server server = new Server(threadPool);
        // 创建一个HTTP的连接，配置监听主机，端口，以及超时时间
        ServerConnector http = new ServerConnector(server);
        http.setHost(conf.getIp());
        http.setPort(Integer.valueOf(conf.getPort()));
        http.setIdleTimeout(-1);		//设置超时时间（无超时）
        server.addConnector(http);		        // 将此连接添加到Server
		context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.getSessionHandler().getSessionManager().setMaxInactiveInterval(30);
		context.setContextPath(conf.getRoot());
		//context.setMaxFormContentSize(-1);
		File directory = new File("");//参数为空 
		String courseFile = directory.getCanonicalPath() ; 
		context.setResourceBase(courseFile + "\\res");
		casePath = context.getResourceBase();
		basicPathLength = casePath.length() -7;
				
		QuerryPart querryPart = new QuerryPart();
		GetTaskPart getTaskPart = new GetTaskPart();
		ReportPart reportPart = new ReportPart();
		StartPart startPart = new StartPart();
		EndPart endPart = new EndPart();
		StartRemotePart startRemotPart = new StartRemotePart();
		ReportStatusPart reportStatusPart = new ReportStatusPart();
		QuerryResultPart querryResultPart = new QuerryResultPart();
		SetBlackListPart blackListPart = new SetBlackListPart();
		QuerryBlackListPart querryBlackListPart = new QuerryBlackListPart();
		context.addServlet(new ServletHolder(querryPart), conf.getQuerryDeviceUrl());
		context.addServlet(new ServletHolder(startPart), conf.getStartUrl());
		context.addServlet(new ServletHolder(startRemotPart), conf.getStartRemoteUrl());
		context.addServlet(new ServletHolder(endPart), conf.getStopUrl());
		context.addServlet(new ServletHolder(getTaskPart), conf.getGetTaskUrl());
		context.addServlet(new ServletHolder(reportPart), conf.getReportUrl());
		context.addServlet(new ServletHolder(reportStatusPart), conf.getReportStatusUrl());
		context.addServlet(new ServletHolder(querryResultPart), conf.getGetTaskResultUrl());
		context.addServlet(new ServletHolder(blackListPart), conf.getSetBlacklistUrl());
		context.addServlet(new ServletHolder(querryBlackListPart), conf.getQuerryBlacklistUrl());
        server.setHandler(context);

        // 启动Server
        server.start();
        System.out.println("-----------------------> started");
        server.join();
	}
	
	public static String getCaseRootPath() {
		return casePath;
	}
	
	public static ServletContextHandler getContext() {
		return context;
	}
}
